package com.stylefeng.guns.modular.usermanager.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.warpper.UserWarpper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.UserTest;
import com.stylefeng.guns.modular.usermanager.service.IUserTestService;

import java.util.List;
import java.util.Map;

/**
 * 人员管理控制器
 *
 * @author fengshuonan
 * @Date 2022-07-17 11:09:59
 */
@Controller
@RequestMapping("/userTest")
public class UserTestController extends BaseController {

    private String PREFIX = "/usermanager/userTest/";

    @Autowired
    private IUserTestService userTestService;

    /**
     * 跳转到人员管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "userTest.html";
    }

    /**
     * 跳转到添加人员管理
     */
    @RequestMapping("/userTest_add")
    public String userTestAdd() {
        return PREFIX + "userTest_add.html";
    }

    /**
     * 跳转到修改人员管理
     */
    @RequestMapping("/userTest_update/{userTestId}")
    public String userTestUpdate(@PathVariable Integer userTestId, Model model) {
        UserTest userTest = userTestService.selectById(userTestId);
        model.addAttribute("item",userTest);
        LogObjectHolder.me().set(userTest);
        return PREFIX + "userTest_edit.html";
    }

    /**
     * 获取人员管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public Object list(@RequestParam(required = false) String name) {
        if (ShiroKit.isAdmin()){
            List<Map<String, Object>> usersTest = userTestService.selectUsersTest(null, name);
            return new UserWarpper(usersTest).warp();
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<Map<String, Object>> usersTest = userTestService.selectUsersTest(dataScope, name);
            return new UserWarpper(usersTest).warp();
        }
    }

    /**
     * 新增人员管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(UserTest userTest) {
        userTestService.insert(userTest);
        return SUCCESS_TIP;
    }

    /**
     * 删除人员管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer userTestId) {
        userTestService.deleteById(userTestId);
        return SUCCESS_TIP;
    }

    /**
     * 修改人员管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(UserTest userTest) {
        boolean b = userTestService.updateById(userTest);
        if (b){
            return SUCCESS_TIP;
        }
        return new ErrorTip(500,"修改失败");
    }

    /**
     * 人员管理详情
     */
    @RequestMapping(value = "/detail/{userTestId}")
    @ResponseBody
    public Object detail(@PathVariable("userTestId") Integer userTestId) {
        return userTestService.selectById(userTestId);
    }
}
